package com.zjitc.controller;


import com.zjitc.model.*;
import com.zjitc.service.CarService;
import com.zjitc.service.OrderService;
import com.zjitc.service.ProductService;
import com.zjitc.service.ShippingService;
import com.zjitc.utils.Utils;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/6 0006
 * @description:
 */


/**
 * 由于外键约束，，想删除order表中的数据必须先删除orderItem中的数据
 *
 * @author Administrator
 */
@Controller
@RequestMapping(value = "/order")
public class OrderControl {

    @Autowired
    private CarService carService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ShippingService shippingService;

    @RequestMapping(value = "/orderInfo")
    public String orderInfo(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        Shipping one = shippingService.findOne(user.getUid());
        if (one != null) {
            orderService.carToOrder(session);
            return "redirect:/order/orderList.do?";
        } else {
            model.addAttribute("msg", "请先点击左上角绑定收获地址");
            model.addAttribute("address", "/index.do");
            return "/jsp/msg.jsp";
        }
    }

    @RequestMapping(value = "/orderList")
    public ModelAndView orderList(
            @RequestParam(name = "pageId", defaultValue = "0", required = false) int pageId,
            @RequestParam(name = "pageSize", defaultValue = "4", required = false) int pageSize,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        Page<Order> page = orderService.findByUid(user.getUid(), pageId, pageSize);
//    PageInfo info = new PageInfo(orders);
        ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/order_list.jsp"));
        mv.addObject("page", page);
        return mv;
    }

    @RequestMapping(value = "/deleteByOid")
    @ResponseBody
    public String deleteOrder(String oid) {
        orderService.deleteByOid(oid);
        return oid;
    }

    /**
     * 付款
     */
    @RequestMapping(value = "/orderPay")
    public ModelAndView orderPay(String oid, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Shipping one = shippingService.findOne(user.getUid());
        if (one != null) {
            Order order = orderService.findOrderByOid(oid);
            ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/order_info.jsp"));
            mv.addObject("order", order);
            mv.addObject("shipping", one);
            return mv;
        } else {
            ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/msg.jsp"));
            mv.addObject("msg", "请先点击左上角绑定收获地址");
            mv.addObject("address", "/index.do");
            return mv;
        }
    }

    /**
     * 付款成功后，指向这个url更改订单的状态
     */
    @RequestMapping(value = "/updateOrderState")
    public String updateOrderState(String oid) {
        orderService.updateOrderState(oid);
        return "redirect:/order/orderList.do?";
    }

    @RequestMapping(value = "/reFoundUpdateOrderState")
    @ResponseBody
    public String reFound(String oid) {
        Order order = orderService.findOrderByOid(oid);
        if (order.getState() == 1) {
            order.setState(-1);
            orderService.update(order);
            return Integer.toString(1);
        } else {
            return Integer.toString(-1);
        }
    }

    @RequestMapping(value = "/hello", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String hello() {
        /**
         * 解决单个字符串乱码问题
         */
        return "华盛顿发";
    }

    @RequestMapping(value = "/secKillPayInfo")
    public ModelAndView secKillPayInfo(@RequestParam(name = "pid") String pid,
                                       @RequestParam(name = "count") int count, HttpSession session, HttpServletRequest request) {
        //获取全局已经秒杀产品的数量
        int i = Utils.productCount(request, pid, count);

        User user = (User) session.getAttribute("user");
        if (user == null) {
            ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/msg.jsp"));
            mv.addObject("msg", "请您先登录!");
            mv.addObject("address", "/jsp/login.jsp");
            return mv;
        }
        //int peopleNumber=2;
        /**
         * 接下来的工作
         * 向product表中添加skillProductCount字段
         * 判断秒杀数量是否符合后台管理员定义规则，，
         * 如果合格进行秒杀
         */
        Product product1 = productService.findByPid(pid);
        Integer skillCount = product1.getSkillCount();
        if (i > 0 & i <= skillCount) {
            Shipping one = shippingService.findOne(user.getUid());
            if (one == null) {
                ModelAndView mv = new ModelAndView(new InternalResourceView("/jsp/msg.jsp"));
                mv.addObject("msg", "请先点击左上角绑定收获地址");
                mv.addObject("address", "/index.do");
                return mv;
            }
            Order order = orderService.sckSkillRabbit(pid, count, session);
            ModelAndView mc = new ModelAndView(new InternalResourceView("/jsp/order_info.jsp"));
            mc.addObject("order", order);
            mc.addObject("shipping", one);
            return mc;
        } else {
            ServletContext context = request.getServletContext();
            context.removeAttribute("productSkill");
            Product product = productService.findByPid(pid);
            product.setFlag(0);
            product.setSkillCount(0);
            productService.update(product);
            ModelAndView mc = new ModelAndView(new InternalResourceView("/jsp/msg.jsp"));
            mc.addObject("msg", "该商品秒杀活动已经结束");
            mc.addObject("address", "/index.do");
            return mc;
        }
    }
}
